package com.global.college.ui.fragment.mine;

import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.global.college.R;
import com.global.college.adapter.InvitePaidAdapter;
import com.global.college.api.AuthRequestClient;
import com.global.college.api.ProgressSubscriber;
import com.global.college.base.BaseFragment;
import com.global.college.constants.Constant;
import com.global.college.ui.bean.InviteHistoryModel;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 收藏
 */
public class InvitePaidFragment extends BaseFragment implements OnRefreshListener, OnLoadmoreListener {

    @BindView(R.id.mRecycler)
    RecyclerView mRecycler;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.ll_empty_view)
    LinearLayout llEmptyView;
    @BindView(R.id.ll_content_view)
    LinearLayout llContentView;

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    public int page = Constant.page;
    public int pageSize = Constant.pageSize;
    private InvitePaidAdapter adapter;


    public InvitePaidFragment() {
        // Required empty public constructor
    }


    public static InvitePaidFragment newInstance(String param1, String param2) {
        InvitePaidFragment fragment = new InvitePaidFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }


    @Override
    public int getContentViewId() {
        return R.layout.fragment_invite_list;
    }

    @Override
    public void initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        smartRefreshLayout.setOnRefreshListener(this);
        smartRefreshLayout.setOnLoadmoreListener(this);
//        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        mRecycler.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new InvitePaidAdapter();
        adapter.bindToRecyclerView(mRecycler);
        smartRefreshLayout.setEnableRefresh(false);
    }

    @Override
    protected void initData() {
        getDataFromNet();
    }

    @OnClick({R.id.btn_invite_jump})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_invite_jump:
                mActivity.finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        page++;
        getDataFromNet();
        smartRefreshLayout.finishLoadmore();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        page = 1;
        smartRefreshLayout.setEnableLoadmore(true);
        adapter = new InvitePaidAdapter();
        getDataFromNet();
    }

    /**
     * 获取数据
     */
    private void getDataFromNet() {
        AuthRequestClient.getInstance().getInvitePaidList(page, pageSize).subscribe(new ProgressSubscriber<InviteHistoryModel>(mActivity, true) {
            @Override
            public void onNext(InviteHistoryModel listModel) {
                addData(listModel, page);
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        });
    }

    public void addData(InviteHistoryModel listModel, int page) {
        smartRefreshLayout.finishRefresh();
        if (page == 1 && (listModel == null||listModel.getList() == null || listModel.getList().size() == 0)) {
            llEmptyView.setVisibility(View.VISIBLE);
            llContentView.setVisibility(View.GONE);
            return;
        }
        if (listModel.getList() == null || listModel.getList().size() == 0) {
            smartRefreshLayout.setEnableLoadmore(false);
            return;
        }
        adapter.addData(listModel.getList());
    }

}